<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../res/layui/css/layui.css" media="all">
    <script src="../../res/layui/layui.js" charset="utf-8"></script>
    <script src="../../res/js/jquery-3.1.1.min.js" charset="utf-8"></script>
    <script src="../../res/js/Tools.js" charset="utf-8"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
</head>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 5px;">
    <legend>增加和修改</legend>
</fieldset>

<form class="layui-form" action="" lay-filter="examplet" id="newsForm">


    <input type='hidden' name='resultId' id='resultId'>

    <div class="layui-form-item">
        <label class="layui-form-label">请选择对手</label>

        <div class="layui-input-block" style="width:200px;">

            <select name="rivId" id="rivId" lay-verify="required">

             <option value=""></option>

            </select>

        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label"> 比赛名称</label>
        <div class="layui-input-block">
            <input type="text" name="matchName" lay-verify="title" autocomplete="off" placeholder="请输入比赛名称" class="layui-input" id="matchName">
        </div>
    </div>

    <div class="layui-inline">
        <label class="layui-form-label">比赛时间</label>
        <div class="layui-input-inline">
            <!--HH:mm:ss-->
            <input type="text" name="matchTime" class="layui-input" id="matchTime" placeholder="选择时间">
            <br>
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">比赛地点</label>
        <div class="layui-input-block">
            <input type="text" name="matchAddr" lay-verify="title" autocomplete="off" placeholder="请输入比赛地点" class="layui-input" id="matchAddr">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">比赛项目</label>
        <div class="layui-input-block">
            <input type="text" name="matchItem" lay-verify="title" autocomplete="off" placeholder="请输入比赛项目" class="layui-input" id="matchItem">
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">比赛成绩</label>
        <div class="layui-input-block">
            <input type="text" name="matchResult" lay-verify="title" autocomplete="off" placeholder="请输入比赛成绩" class="layui-input" id="matchResult">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">比赛排名</label>
        <div class="layui-input-block">
            <input type="text" name="matchSort" lay-verify="title" autocomplete="off" placeholder="请输入比赛排名" class="layui-input" id="matchSort">
        </div>
    </div>

    <!--<div class="layui-form-item">-->
        <!--<label class="layui-form-label">积分</label>-->
        <!--<div class="layui-input-block">-->
            <!--<input type="text" name="score" lay-verify="title" autocomplete="off" placeholder="请输入积分" class="layui-input" id="score">-->
        <!--</div>-->
    <!--</div>-->


    <!--<div class="layui-form-item">-->
    <!--<label class="layui-form-label">是否有效</label>-->
    <!--<div class="layui-input-block">-->
    <!--<input type="checkbox" name="isva" checked="" id="isva" lay-skin="switch" lay-text="有效|无效">-->
    <!--</div>-->
    <!--</div>-->

    <!--<div class="layui-form-item">-->
        <!--<label class="layui-form-label">备注</label>-->
        <!--<div class="layui-input-block">-->
            <!--<input type="text" name="remark" id="remark" lay-verify="title" autocomplete="off" placeholder="备注" class="layui-input">-->
        <!--</div>-->
    <!--</div>-->


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo2">立即提交</button>
        </div>
    </div>

</form>

<script>

    layui.use(['form', 'layedit', 'laydate','upload','laydate'], function(){

        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,laydate = layui.laydate
            ,upload = layui.upload ;


        //日期时间选择器
        laydate.render({
            elem: '#matchTime'
            ,type: 'datetime'
            ,format: 'yyyy-MM-dd HH:mm:ss' //格式
            ,istime: true   //是否开启选择
            ,value: new Date()//默认值


        });

        //得到上个页面行JSON字符串
        parent_json =  (parent.JonData);

         //不等于空时截取
         if(parent_json!=null){

              console.info(parent_json);

             //截取json字符串
             var id = parent_json.slice(9,19);

             ids =  id.replace(/[\ |\~|\`|\!|\@|\#|\$|\%|\^|\&|\*|\(|\)|\-|\_|\+|\=|\||\\|\[|\]|\{|\}|\;|\:|\"|\'|\,|\<|\.|\>|\/|\?]/g,"");

             var reg = /[a-zA-Z]/g;

             idss= ids.replace(reg,"");

             $.ajax({
                 asyn: true,
                 type: "post",
                 url: "../../AtsRival/optipon",
                 data: {

                     rivId: idss
                 },
                 success: function (msg) {

                     if(msg.length>0){

                         //清除之前的哦option
                         $("#rivId").empty();

                         for (var i = 0; i < msg.length; i++) {
                             //动态添加
                             $("#rivId").append("<option value=\"" + msg[i].rivId + "\">" + msg[i].rivName + "<option>");
                         }
                         //重新渲染
                         layui.form.render("select");

                     }

                 },
                 error: function (fh) {

                 }
             });


         }else{

             var id = getQueryString("resultId");
             if(id!=null){

                 var url = "../../AtsRivalResult/findById";
                 var data = {id:id};
                 $.post(url,data,function(obj){

                     $("#resultId").val(obj.resultId);
                     $("#matchName").val(obj.matchName);
                     $("#matchTime").val(obj.matchTime);
                     $("#matchAddr").val(obj.matchAddr);
                     $("#matchItem").val(obj.matchItem);
                     $("#matchResult").val(obj.matchResult);
                     $("#matchSort").val(obj.matchSort);
                     // $("#score").val(obj.score);
                     // $("#remark").val(obj.remark);

                 },'json');


                 var url = "../../AtsRivalResult/findByIdriv";
                 var data = {id:id};
                 $.post(url,data,function(obj){


                     //清除之前的哦option
                     $("#rivId").empty();

                     $('#rivId').append("<option value='"+obj.rivId+"'>"+obj.rivName+"</option>");

                     layui.form.render("select");



                 },'json')



             }
         }




        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');
        //自定义验证规则
        form.verify({
            title: function(value){
                if(value.length < 1){
                    return '名字至少得1个字符啊';
                }
            }
            ,content: function(value){
                layedit.sync(editIndex);
            }
        });
        //监听提交
        form.on('submit(demo2)', function(data){

            var formData = new FormData(document.getElementById("newsForm"));
            //传输路径
            var url = "../../AtsRivalResult/AddOrUpdate";
            $.ajax({
                url:url,
                type:"post", // 传输类型
                data:formData, // 数据
                async:false, // 全局同步
                processData:false,
                contentType:false,
                dataType:"json",
                success:function(obj){

                    if(obj.num==1){
                        layer.msg(obj.msg,{
                            icon:1,
                            time:1000,
                        },function(){
                            var indexs = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(indexs);
                        });
                        return false ;
                    }
                },
                error:function(e){
                    alert("失败了");
                }
            });
            return false
        });


        var id = getQueryString("resultId");

        if(id!=null){

            var url = "../../AtsRivalResult/findById";
            var data = {id:id};
            $.post(url,data,function(obj){
                $("#resultId").val(obj.resultId);
                $("#matchName").val(obj.matchName);
                $("#matchTime").val(obj.matchTime);
                $("#matchAddr").val(obj.matchAddr);
                $("#matchItem").val(obj.matchItem);
                $("#matchResult").val(obj.matchResult);
                $("#matchSort").val(obj.matchSort);
                // $("#score").val(obj.score);
                // $("#remark").val(obj.remark);

            },'json')

        }

    });


</script>
</body>
</html>